﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
namespace project
{
    public partial class ControlBook : BookPage
    {
        public ControlBook()
        {
            InitializeComponent();
            this.Size = new System.Drawing.Size(1271, 631);
            this.uiDataGridView1.Size = new System.Drawing.Size(1201, 561);
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            // 添加书
            InputBookPage a = new InputBookPage();
            a.ShowDialog();
            QueryAllBook();
        }

        private void uiButton4_Click(object sender, EventArgs e)
        {
            // 修改书
            int selectCount = this.uiDataGridView1.SelectedRows.Count;
            if (selectCount != 0)
            {
                string id = uiDataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string BookName = uiDataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                string BookAuthor = uiDataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                string category = uiDataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                string Publisher = uiDataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                string Store = uiDataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                string location = uiDataGridView1.SelectedRows[0].Cells[6].Value.ToString();
                string has = uiDataGridView1.SelectedRows[0].Cells[7].Value.ToString();
                EditBook adminEditForm = new EditBook(id, BookName, BookAuthor,category ,Publisher, Store,location,has);
                adminEditForm.ShowDialog();
                QueryAllBook();
            }
            else
            {
                MessageBox.Show("请选择正确的数据", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            //删除书
            int selectCount = uiDataGridView1.SelectedRows.Count;
            if (selectCount != 0)
            {
                if (MessageBox.Show("你确认要删除当前选中行吗？此操作不可回退", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    int id = int.Parse(uiDataGridView1.SelectedRows[0].Cells[0].Value.ToString());

                    string sqlStr = $"delete from books where number={id}";

                    int result = SqlHelper.ExecuteNonQuery(sqlStr);
                    if (result != 0)
                    {
                        MessageBox.Show("删除成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        QueryAllBook();
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择正确的数据", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void uiButton5_Click(object sender, EventArgs e)
        {
             // 借书   
            int selectCount = uiDataGridView1.SelectedRows.Count;
            RentBook a;
            if (selectCount != 0)
            {
                int book_id = int.Parse(uiDataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                a = new RentBook(book_id.ToString());
            }
            else
            {
                MessageBox.Show("请选择正确的数据", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                a = new RentBook();
            }
            a.ShowDialog();
            QueryAllBook();
        }
    }
}
